// DBDeployer - The MySQL Sandbox
// Copyright © 2006-2020 Giuseppe Maxia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sandbox

// Templates for group replication

var (
	pxcStartTemplate string = `#!{{.ShellPath}}
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using {{.TemplateName}} on {{.DateTime}}
SBDIR={{.SandboxDir}}
echo "# executing 'start' on $SBDIR"
{{ range .Nodes }}
echo 'executing "start" on {{.NodeLabel}} {{.Node}}'
$SBDIR/{{.NodeLabel}}{{.Node}}/start {{if eq .Node 1}} --wsrep-new-cluster {{end}} "$@"
{{end}}
`

	checkPxcNodesTemplate string = `#!{{.ShellPath}}
{{.Copyright}}
# Generated by dbdeployer {{.AppVersion}} using {{.TemplateName}} on {{.DateTime}}
multi_sb={{.SandboxDir}}
[ -z "$SLEEP_TIME" ] && SLEEP_TIME=1

CHECK_NODE="SHOW GLOBAL STATUS WHERE variable_name IN"
CHECK_NODE="$CHECK_NODE ('wsrep_local_state','wsrep_local_state_comment',"
CHECK_NODE="$CHECK_NODE 'wsrep_local_commits','wsrep_received',"
CHECK_NODE="$CHECK_NODE 'wsrep_cluster_size','wsrep_cluster_status',"
CHECK_NODE="$CHECK_NODE 'wsrep_local_state_uuid','wsrep_incoming_addresses',"
CHECK_NODE="$CHECK_NODE 'wsrep_gcomm_uuid','wsrep_connected','wsrep_ready')"

{{ range .Nodes}}
	echo "# Node {{.Node}}"
	$multi_sb/{{.NodeLabel}}{{.Node}}/use -t -e "$CHECK_NODE"
	sleep $SLEEP_TIME
{{end}}
`
	pxcReplicationTemplate string = `
# These options, after customization, are added to my.sandbox.cnf
innodb_file_per_table
innodb_autoinc_lock_mode=2
wsrep-provider={{.Basedir}}/lib/libgalera_smm.so
wsrep_cluster_address={{.GroupCommunication}}
wsrep_node_incoming_address=127.0.0.1
wsrep_provider_options=gmcast.listen_addr=tcp://127.0.0.1:{{.GroupPort}}
wsrep_sst_method={{.SstMethod}}
#pxc_encrypt_cluster_traffic=off
{{.PxcEncryptClusterTraffic}}
loose_wsrep_sst_auth=root:
wsrep_node_address={{.NodeIp}}
innodb_flush_method=O_DIRECT
core-file
secure-file-priv=
loose-innodb-status-file=1
log-output=none
wsrep_slave_threads=2
wsrep_sst_receive_address=127.0.0.1:{{.RsyncPort}}
`
	PxcTemplates = TemplateCollection{
		"check_pxc_nodes_template": TemplateDesc{
			Description: "Checks the status of PXC replication",
			Notes:       "",
			Contents:    checkPxcNodesTemplate,
		},
		"pxc_replication_template": TemplateDesc{
			Description: "Replication options for PXC",
			Notes:       "",
			Contents:    pxcReplicationTemplate,
		},
		"pxc_start_template": TemplateDesc{
			Description: "start all nodes in a PXC",
			Notes:       "",
			Contents:    pxcStartTemplate,
		},
	}
)
